package com.ciwong.epaper.modules.pad.homeworkwidget.presenter;

import android.content.Context;
import android.content.Intent;

import com.ciwong.epaper.R;
import com.ciwong.epaper.modules.me.ui.BindPhoneActivity;
import com.ciwong.epaper.modules.pad.contentprovider.LoginToken;
import com.ciwong.epaper.modules.pad.homeworkwidget.common.AppWidgetConstants;
import com.ciwong.epaper.modules.pad.homeworkwidget.widget.remoteviews.HomeworkRemoteViewsDecorator;
import com.ciwong.epaper.util.IntentFlag;
import com.ciwong.libs.utils.CWLog;

import static com.ciwong.mobilelib.utils.BaseJumpManager.GO_BACK_ID;

public class ReceverPresenter {

    private String TAG="ReceverPresenter";
    private static ReceverPresenter receverPresenter;

    private ReceverPresenter(){

    }
    public static ReceverPresenter getInstance(){
        if (receverPresenter==null)
        receverPresenter = new ReceverPresenter();
        return receverPresenter;
    }


    /**
     * 退出登录
     *
     * @param context
     * @param intent
     */
    public void logoutSuccess(Context context, Intent intent) {
        HomeworkRemoteViewsDecorator.getInstance().checkIsLogin(context,false,false, false);
    }

    /**
     * 登录成功(退出后或者首次登陆成功app中发送的广播状态)
     *
     * @param context
     * @param intent
     */

    public void appLoginSuccess(Context context, Intent intent) {
        boolean flag_widget_bind_phone = intent.getBooleanExtra(LoginToken.IS_BIND_PHONE,false);
        if (flag_widget_bind_phone){
            loginSuccess(context, intent,true);
        } else{
            jumpToBindPhone(context);
        }
    }


    /**
     * widget中点击事件判断到登陆成功
     *
     * @param context
     * @param intent
     */
    public void widgetLoginSuccess(Context context, Intent intent) {
        //是否绑定了手机号
        boolean flag_widget_bind_phone = intent.getBooleanExtra(AppWidgetConstants.FLAG_WIDGET_BIND_PHONE,false);
        if (flag_widget_bind_phone){
            loginSuccess(context, intent,true);
        } else{
            jumpToBindPhone(context);
        }
    }
    /**
     * 绑定手机号成功（一定登录成功）
     *
     * @param context
     * @param intent
     */

    public void bindPhoneSuccess(Context context, Intent intent) {
        loginSuccess(context, intent,true);
    }
    private void loginSuccess(Context context, Intent intent,boolean isBindPhone) {
        boolean islogin = true;
        boolean isJoinedClass = false;

        if (HomeworkWidgetCheckState.isJoinClass()) {//本地验证加入班成功
            isJoinedClass = true;
        } else {//本地验证加入班级失败
            HomeworkWidgetCheckState.checkNetIsJoinClass(context);
        }
        HomeworkRemoteViewsDecorator.getInstance().checkIsLogin(context, islogin,isBindPhone, isJoinedClass);
//        登陆成功刷新个人信息
        PersonalInfor.getInstance(context).refreshPerInfor(islogin, isBindPhone);
    }


    /**
     * 加入班级成功
     *
     * @param context
     * @param intent
     */
    public void joinInClassSuccess(Context context, Intent intent) {
        hasJoinedInClassSuccess(context, intent);
    }

    /**
     * 已经加入班级
     *
     * @param context
     * @param intent
     */
    public void hasJoinedInClassSuccess(Context context, Intent intent) {
        CWLog.d(TAG,"lzh 接到广播： 加入班级成功");
        PersonalInfor.getInstance(context).refreshPerInfor(true, true);
        getLoadLocalHomeworks(context);//请求数据（本地加网络）
        HomeworkRemoteViewsDecorator.getInstance().checkIsLogin(context, true,true,true);
    }

    /**
     * 刷新listview
     *
     * @param context
     * @param intent
     */

    public void refreshListData(Context context, Intent intent) {
        HomeworkRemoteViewsDecorator.getInstance().refreshListData(context);
    }


    /**
     * 加载本地数据
     *
     * @param context
     */
    private void getLoadLocalHomeworks(Context context) {
        DataProvider.loadLocalHomeworks(context);
    }

    /**
     * 加载网络数据
     *
     * @param context
     */
    public void getNetHomeworklist(Context context) {
        DataProvider.getMyWorkList(context);
    }

    private void jumpToBindPhone(Context context) {
        Intent newIntent = new Intent(context,  BindPhoneActivity.class);
        newIntent.putExtra(GO_BACK_ID, R.string.go_back);
        newIntent.putExtra(IntentFlag.INTENT_FLAG_STR, "");
        newIntent.putExtra(IntentFlag.INTENT_FLAG_ID, "2");//代表登录绑定
        newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
        context.startActivity(newIntent);
    }
}
